package com.example.springbootdemo.controller;

import com.example.springbootdemo.model.ChooseInfo;
import com.example.springbootdemo.model.ResInfo;
import com.example.springbootdemo.service.StatisticService;
import com.example.springbootdemo.util.WebUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/statistic")
@Controller
public class Microcontroller {

    @Resource
    private StatisticService statisticService;

    @PostMapping("/do/{choose}")
    @ResponseBody
    public ResInfo<ChooseInfo> statistic(@PathVariable("choose") String choose) {

        String ip = WebUtil.getIp();
        ChooseInfo statistic = statisticService.statistic(ip, choose);
        if (statistic == null) {
            return new ResInfo<>(1, "选择成功", null);
        }

        return new ResInfo<>(-1, "已经选过了", statistic);
    }

    @GetMapping("/result/xyz")
    public String showResult(Model model) {
        model.addAllAttributes(statisticService.getStatistic());
        return "staticsData";
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }
}
